using NUnit.Framework;
using Sandia.MVCFramework.Common;
using Sandia.MVCFramework.Data;
using Sandia.MVCFramework.ExportTools;
using Sandia.MVCFramework.Request;
using System;
using System.Collections.Generic;
using System.Text;

namespace Sandia.MVCFramework.Test
{
    [TestFixture]
    public class ExportToolsTests
    {
        [Test]
        public void ContainerTest()
        {
            TestData payload = new TestData();
            Container container = new Container(payload);
            Assert.AreEqual(payload, container.Payload);
        }

        [Test]
        public void ExportContainerTest()
        {
            TestData payload = new TestData();
            string filename = "filename";
            string filemask = "filemask";

            ExportContainer exportContainer = 
                new ExportContainer(payload, filename, filemask);

            Assert.AreEqual(filename, exportContainer.Filename);
            Assert.AreEqual(filemask, exportContainer.FileMask);
        }

        [Test]
        public void ImportContainerTest()
        {
            TestData payload = new TestData();
            ImportContainer importContainer = new ImportContainer(payload);
            Assert.AreEqual(payload, importContainer.Payload);
        }

        [Test]
        public void AbstractFileImporterTest()
        {
            TestFileImporter fileImporter = new TestFileImporter();

            Assert.AreEqual(1, fileImporter.DesiredRequests.Count);
            Assert.IsTrue(fileImporter.DesiredRequests.Contains(typeof(ImportRequest)));
            
            TestPublisher publisher = new TestPublisher();
            fileImporter.InitDataPacketProducer(publisher);
            Assert.AreEqual(publisher, fileImporter.Publisher);

            ImportRequest request = new ImportRequest();
            request[ImportFilenameRequestParameter.NAME] = new ImportFilenameRequestParameter("filename");
            fileImporter.ProcessRequest(request);
            Assert.AreEqual(fileImporter.Data, publisher.LastPublishedData.Data);

            Assert.IsFalse(fileImporter.IsDisposed);
            fileImporter.Disposed += new EventHandler(fileImporter_Disposed);
            fileImporter.Dispose();
            Assert.IsTrue(fileImporter.IsDisposed);
            Assert.IsTrue(this.fileImporterDisposed);
        }

        void fileImporter_Disposed(object sender, EventArgs e)
        {
            this.fileImporterDisposed = true;
        }

        private bool fileImporterDisposed = false;

        public class TestPublisher : IPublisher
        {

            #region IPublisher Members

            public void Subscribe(ISubscriber subscriber, Type dataType)
            {
                throw new Exception("The method or operation is not implemented.");
            }

            public void RemoveSubscription(ISubscriber subscriber, Type dataType)
            {
                throw new Exception("The method or operation is not implemented.");
            }

            private DataPacket lastPublishedData;

            public DataPacket LastPublishedData
            {
                get
                {
                    return this.lastPublishedData;
                }
            }

            public void PublishData(
                DataPacket dataPacket)
            {
                this.lastPublishedData = dataPacket;
            }

            #endregion
        }

        public class TestFileImporter : AbstractFileImporter
        {
            public IPublisher Publisher
            {
                get
                {
                    return this.publisher;
                }
            }

            private TestData data = new TestData();

            public IData Data
            {
                get { return this.data; }
            }

            protected override IData processRequest(
                string filename)
            {
                return this.data;
            }
        }
    }
}
